import { AppStaticData } from '@/initialization/InitAppStaticData'
import { RouteGroup } from '@/utils/route-group'
import type { Router } from 'vue-router'

export const AuthRouter = new RouteGroup([
  {
    path: '/IssueStorage',
    name: 'IssueStorage',
    meta: { requiresAuth: true },
    component: () => import('@/pages/IssueStorage/IssueStorage.vue'),
  },
])

export function beforEachAuthRouter(router: Router) {
  router.beforeEach((to) => {
    if (!AuthRouter.match(to)) return
    if (!AppStaticData.username) {
      return { name: 'Login', query: { redirect: to.fullPath } }
    }
    if (AuthRouter.addRoute(router)) {
      return to.fullPath
    }
  })
}
